﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;
using Zyg.SSO.Core.Business;
using System.Data;

namespace Zyg.SSO.Web.Admin.App
{
    public partial class AppAccounts : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!base.Account.HavePermission(Permission.AppManage))
                {
                    throw new Exception("你无权查看此页面");
                }
                bool parseSucceed;

                Guid id = Request.QueryString["AppId"].ToGuid(out parseSucceed);

                if (parseSucceed)
                {
                    BindData(id);
                }
                else
                {
                    throw new Exception("对不起，你所访问的页面不存在");
                }
            }
        }

        protected void BindData(Guid appId)
        {
            Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(appId);

            if (app == null)
            {
                throw new Exception("对不起，你所访问的页面不存在");
            }

            AppName.Text = AppName1.Text = app.Name;

            IList<Zyg.SSO.Core.Business.Account> appAccounts = new List<Zyg.SSO.Core.Business.Account>();
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("AccountEmail");
            dt.Columns.Add("AddTime");

            for (int i = 0; i < app.Accounts.Count; i++)
            {
                appAccounts.Add(app.Accounts[i].Account);
                dt.Rows.Add(app.Accounts[i].Id, app.Accounts[i].Account.Email, app.Accounts[i].DateCreated);
            }

            GridViewAppAccounts.DataSource = dt;
            GridViewAppAccounts.DataBind();

            IList<Zyg.SSO.Core.Business.Account> allAccounts = Zyg.SSO.Core.Business.Account.GetAccounts(false, Zyg.SSO.Core.Business.AccountState.Active);
            IEnumerable<Zyg.SSO.Core.Business.Account> notAppAccounts = from account in allAccounts
                                                                        where !appAccounts.Contains(account)
                                                                        select account;

            GridViewNotAppAccounts.DataSource = notAppAccounts;
            GridViewNotAppAccounts.DataBind();
        }

        /// <summary>
        /// 添加用户到应用
        /// </summary>
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            bool parseSucceed;

            Guid id = Request.QueryString["AppId"].ToGuid(out parseSucceed);

            Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(id);

            if (app == null)
                return;

            for (int i = GridViewNotAppAccounts.Rows.Count - 1; i > -1; i--)
            {
                CheckBox cb = GridViewNotAppAccounts.Rows[i].FindControl("CBSelect") as CheckBox;

                if (cb.Checked)
                {
                    AccountApp accountApp = new AccountApp();

                    accountApp.Id = Guid.NewGuid();
                    Guid accountId = (System.Guid)GridViewNotAppAccounts.DataKeys[i].Value;
                    accountApp.AccountID = accountId;
                    accountApp.AppID = app.Id;
                    accountApp.DateCreated = DateTime.Now;
                    accountApp.ExpireTime = DateTime.Now.AddMonths(1);

                    app.Accounts.AddOnSave(accountApp);
                }
            }

            app.Save();

            BindData(app.Id);
        }

        /// <summary>
        /// 删除应用的用户
        /// </summary>
        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            bool parseSucceed;

            Guid id = Request.QueryString["AppId"].ToGuid(out parseSucceed);

            Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(id);

            if (app == null)
                return;

            for (int i = GridViewAppAccounts.Rows.Count - 1; i > -1; i--)
            {
                CheckBox cb = GridViewAppAccounts.Rows[i].FindControl("CBSelect") as CheckBox;

                if (cb.Checked)
                {
                    // Guid accountId = (System.Guid)GridViewAppAccounts.DataKeys[i].Value;
                    // AccountApp accountApp = AccountApp.Load(accountId, app.Id);
                    Guid accountAppId = new Guid(GridViewAppAccounts.DataKeys[i].Value.ToString());
                    AccountApp accountApp = AccountApp.Load(accountAppId);
                    app.Accounts.DeleteOnSave(accountApp);
                }
            }

            app.Save();

            BindData(app.Id);
        }
    }
}
